package com.ck.ips.service.impl.program;

import com.ck.common.model.OperationResult;
import com.ck.common.model.ProgramLog;
import com.ck.common.page.PageInfo;
import com.ck.core.context.UserContextHolder;
import com.ck.ips.dao.program.ProgramLogMapper;
import com.ck.ips.model.vo.program.ProgramLogVO;
import com.ck.ips.service.program.ProgramLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linqiang
 * @version v1.0
 * @Description 节目操作日志Service实现类
 * @date 2019-04-29 16:46
 */
@Service(value = "programLogService")
public class ProgramLogServiceImpl implements ProgramLogService {

    @Autowired
    private ProgramLogMapper programLogMapper;

    /**
     * 新增
     *
     * @param record 新增数据
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insert(ProgramLog record) {
        return programLogMapper.insert(record);
    }

    /**
     * 新增操作成功日志
     *
     * @param programVersion 节目版本号
     * @param operationType  操作内容
     * @return 影响行数
     */
    @Override
    public int insertSuccessLog(String programVersion, String operationType) {
        ProgramLog programLog = new ProgramLog();
        programLog.setProgramVersion(programVersion);
        programLog.setOperationType(operationType);
        programLog.setAddTime(System.currentTimeMillis());
        programLog.setUpdateTime(System.currentTimeMillis());
        programLog.setUserName(UserContextHolder.currentUser().getUserName());
        programLog.setRealname(UserContextHolder.currentUser().getRealName());
        return programLogMapper.insert(programLog);
    }

    /**
     * 新增操作成功日志
     *
     * @param programVersion    节目版本号
     * @param operationUserName 操作人
     * @param operationType     操作内容
     * @return 影响行数
     */
    @Override
    public int insertSuccessLog(String programVersion, String operationUserName, String operationType) {
        ProgramLog programLog = new ProgramLog();
        programLog.setProgramVersion(programVersion);
        programLog.setOperationType(operationType);
        programLog.setAddTime(System.currentTimeMillis());
        programLog.setUpdateTime(System.currentTimeMillis());
        programLog.setUserName(operationUserName);
        programLog.setRealname(operationUserName);
        return programLogMapper.insert(programLog);
    }

    /**
     * 新增操作成功日志
     *
     * @param programVersionList 节目版本号
     * @param operationType      操作内容
     * @return 影响行数
     */
    @Override
    public int batchInsertSuccessLog(List<String> programVersionList, String operationType) {
        List<ProgramLog> programLogList = new ArrayList<>();
        long currentTimeMillis = System.currentTimeMillis();
        for (String programVersion : programVersionList) {
            ProgramLog programLog = new ProgramLog();
            programLog.setProgramVersion(programVersion);
            programLog.setOperationType(operationType);
            programLog.setAddTime(currentTimeMillis);
            programLog.setUpdateTime(currentTimeMillis);
            programLog.setUserName(UserContextHolder.currentUser().getUserName());
            programLog.setRealname(UserContextHolder.currentUser().getRealName());
            programLogList.add(programLog);
        }
        return programLogMapper.batchInsert(programLogList);
    }

    /**
     * 分页列表
     *
     * @param programVersion 节目版本号
     * @param pageNum        当前页
     * @param pageSize       每页条数
     * @return OperationResult
     */
    @Override
    public OperationResult listByPage(String programVersion, Integer pageNum, Integer pageSize) {
        if (StringUtils.isBlank(programVersion)) {
            return OperationResult.newValidateFailOperationResult("节目版本号不能为空");
        }
        PageHelper.startPage(pageNum, pageSize);
        Page<ProgramLogVO> page = programLogMapper.listByPage(programVersion);
        return OperationResult.newSuccessOperationResult(PageInfo.convert(page));
    }
}
